#include "delta.h"


int fab(int** sb, int a, int b) {
	int delta = 0, x, F_x, F_apx;


	int indices[64][2];
	for (int i = 0; i < 64; i++) {
		indices[i][0] = (i&1) | ((i & 32) >> 4);
		//printf("%i\t%i\t%i\n", indices[i][0], (i&32)>>4, i);
		indices[i][1] =
		    ((i & 2) >> 1) | ((i & 4) >> 1) | ((i & 8) >> 1) | ((i & 16)
									>> 1);
		//printf("%i\n", indices[i][1]);
	}

	for (x = 0; x < 64; x++) {
		F_x = sb[indices[x][0]][indices[x][1]];
		F_apx = sb[indices[a^x][0]][indices[a^x][1]];
		//if(a==63) printf("F_x : %i\n", F_x);
		if ((F_x ^ F_apx) == b) {
			delta += 1;
		}
	}
return delta;
}




int delta(int **sb)
{

	int max_delta = 0, delta;
	int a, b;

	for (a = 1; a < 64; a++) {

		for (b = 0; b < 16; b++)  {
			delta = fab(sb,a,b);
			if (delta > max_delta)
				max_delta = delta;
		}
	}
	
	//printf("max_delta : %i\n", max_delta);

	return max_delta;
}
